package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbOrder;
import com.bwie.pojo.TbUser;
import com.bwie.service.IUserService;
import com.bwie.service.TbOrderService;
import com.bwie.mapper.TbOrderMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
* @author Lenovo
* @description 针对表【tb_order(订单表)】的数据库操作Service实现
* @createDate 2025-04-21 16:01:20
*/
@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder>
    implements TbOrderService{

    @Autowired
    TbOrderMapper tbOrderMapper;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    IUserService iUserService;

    @Override
    public R getOrderByOrderId(Integer orderId) {
        TbOrder tbOrder = tbOrderMapper.selectById(orderId);
        if(tbOrder == null){
            return R.ERROR(404,"该订单不存在");
        }

        //使用restTempate对象进行远程调用
//        String url = "http://localhost:9001/user/index/getUserByUserId/"+tbOrder.getUserId();
//        R r = restTemplate.postForObject(url, null, R.class);
//        String s = JSON.toJSONString(r.getData());
//        TbUser tbUser = JSON.parseObject(s, TbUser.class);

        //使用openFeign远程调用
        R r = iUserService.getUserByUserId(tbOrder.getUserId());
        if(r.getCode() != 200){
            return r;
        }
        String s = JSON.toJSONString(r.getData());
        TbUser tbUser = JSON.parseObject(s, TbUser.class);

        tbOrder.setTbUser(tbUser);
        return R.OK(tbOrder);
    }
}




